/**
 * 
 */
package com.nari.heatshare.dao.springjdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.nari.coreframe.dao.springjdbc.AbstractDaoImpl;
import com.nari.coreframe.dao.springjdbc.extendcore.ResultSetMapHandler;
import com.nari.heatshare.utils.excelexport.heatcharge.vo.AreaResultBillListVo;

/**
 * 
 * @author Xiaopeng
 * @date 2015-4-2
 */
public class UserShareResultJdbcDaoImpl extends AbstractDaoImpl 
		implements IUserShareResultJdbcDao {
	private Logger logger = Logger.getLogger(TempPointDataJdbcDaoImpl.class);

	/**
	 * 查询分摊域分摊账单列表信息（分摊域下的用户账单）摘要
	 * @param areaResultUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-4-2
	 */
	public List<AreaResultBillListVo> queryAreaResultExportList(String areaResultUID) throws Exception {
		String querySQL = "select (@rownum:=@rownum+1) as rowNum," +
								 "b.t_building_no as belongBuildingNo," +
								 "sur.t_house_no as houseNo," +
								 "sur.t_room_owner as roomOwner," +
								 "(case sur.if_measure_charge when 1 then '不参与' " +
								 							 "when 2 then '参与' " +
								 							 "else '未知' end) as ifMeasureCharge," +
								 "sur.room_measure_charge as roomMeasureCharge," +
								 "sur.total_charge_amount as totalChargeAmount," +
								 "sur.t_share_percent as sharePercent," +
								 "sur.room_base_heatCharge as roomBaseHeatCharge,"+
								 "sur.room_area_charge as roomAreaCharge  "+
						  "from (select @rownum:=0) r,p_share_user_result sur left join base_building_info b " +
						  "on sur.t_building_id=b.ID " +
						  "where sur.area_result_id='"+areaResultUID+"'";
		logger.debug("querySQL:"+querySQL);
		System.out.println("querySQL:" + querySQL);
		//封装查询结果
		final List<AreaResultBillListVo> areaResultList = new ArrayList<AreaResultBillListVo>();
		this.jdbcTemplate.query(querySQL, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				ResultSetMapHandler<AreaResultBillListVo> mapper = new ResultSetMapHandler<AreaResultBillListVo>();
				try {
					List<AreaResultBillListVo> queryList = mapper.mapResultSet2Object(rs, AreaResultBillListVo.class);
					if(queryList != null) {
						areaResultList.addAll(queryList);
					}
				} catch (Exception e) {
					e.printStackTrace();
					logger.error(e.getMessage());
					throw new SQLException(e.getMessage());
				}
			}
		});
		return areaResultList;
	}
}
